package com.qf.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;

/**
 * @Package: com.qf.utils
 * @ClassName: QiniuUpload
 * @Author: lmr
 * @CreateTime: 2021/9/6 15:55
 * @Description:七牛云上传图片工具类
 * @Version: 1.0
 */
public class QiniuUpload {
    /*1.构造一个带指定 Region 对象的配置类(里面参数是七牛云设置的地域简称)
           华东:Region.region0()或者Region.huadong();
           华北:Region.region1()或者Region.huabei();
           华南:Region.region2()或者Region.huanan();*/
    private static Configuration cfg = new Configuration(Region.region1());//(要改的)
    //2.创建上传对象
    private static UploadManager uploadManager = new UploadManager(cfg);
    //3.设置好账号的密钥access_key和secret_key
    private static String access_key = "38F2s22WhnwqhXoJoJxq40rB2UQJg41zxpVuycfw"; //七牛AK(要改的)
    private static String secret_key = "rYHKbOYeiNPpBcgAgaXqZLvP2TVJ6w16INSd4oPj"; //七牛SK(要改的)
    //4.密钥配置
    private static Auth auth = Auth.create(access_key, secret_key);
    //5.设置要上传的空间名-->对应要上传到七牛上你的那个存储空间名路径（自己建文件夹 注意设置公开）
    private static String bucketName = "lmr-2105";//(要改的)
    //6.设置七牛云默认域名(七牛云找)
    private static String qiNiu_domainName = "http://qyztqipqf.hb-bkt.clouddn.com/";//(要改的)

    //简单上传，使用默认策略，只需要设置上传的空间名就可以了
    public static String getUpToken() {
        return auth.uploadToken(bucketName);
    }

    /**
     * 第一种,给定上传文件在本机的具体路径
     *
     * @param filpath  文件路径
     * @param fileName 文件名称
     * @return
     */
    public static String UploadPic(String filpath, String fileName) {
        String upToken = auth.uploadToken(bucketName);
        try {
            Response response = uploadManager.put(filpath, fileName, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            //返回文件在七牛云的存储地址url,可以直接用浏览器访问
            return qiNiu_domainName + fileName;
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        return null;
    }

    /**
     * 第二种,通过MultipartFile方式上传
     *
     * @param file     上传的文件 MultipartFile(注意,参数名要与前端name中的名字一致)
     * @param filename 上传文件的名称
     * @return 返回文件在七牛云的存储地址url, 可以直接用浏览器访问
     * @throws Exception
     */
    public static String updateFile(MultipartFile file, String filename) throws Exception {
        try {
            //获取输入流
            ByteArrayInputStream byteInputStream = new ByteArrayInputStream(file.getBytes());
            String upToken = auth.uploadToken(bucketName);
            try {
                //默认不指定key的情况下，以文件内容的hash值作为文件名
                String key = filename;
                Response response = uploadManager.put(byteInputStream, key, upToken, null, null);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                //上穿的图片链接
                return qiNiu_domainName + putRet.key;
            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                System.err.println(r.bodyString());
                //ignore
            }
        } catch (Exception ex) {
            //ignore
        }
        return "";
    }
}